package com.bosc.automationui.repository;

import com.bosc.automationui.entity.JobHistory;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface JobHistoryRepository extends CrudRepository<JobHistory, String> {

    @Query("SELECT js FROM JobHistory js WHERE SYSDATE - txDate > 1 AND upper(status) = 'FAILED' AND NOT EXISTS(SELECT status FROM JobHistory WHERE jobId=js.jobId AND (upper(status)='DONE' OR upper(status)='RUNNING' OR upper(status)='PENDING' OR upper(status)='READY'))")
    List<JobHistory> getTPlus();

    @Modifying
    @Query(value = "DELETE FROM JobHistory jh WHERE jh.jobName = :job_name")
    void deleteByJobName(@Param("job_name") String jobName);

    List findAllByOrderByTxDateDesc();
}
